package com.xuecheng.content.model.dto;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@ToString
public class TeachplanDto  implements Serializable {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String pname;
    private Long parentid;
    private Integer grade;
    private String mediaType;
    private LocalDateTime startTime;
    private LocalDateTime endTime;
    private String description;
    private String timelength;
    private Integer orderby;
    private Long courseId;
    private Long coursePubId;
    private Integer status;
    private String isPreview;
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createDate;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime changeDate;
    //课程计划关联的媒资信息
    TeachplanMedia teachplanMedia;

    //子结点
    List<TeachplanDto> teachPlanTreeNodes;

    public TeachplanDto() {
    }

    public TeachplanDto(Long id, String pname, Long parentid, Integer grade, String mediaType, LocalDateTime startTime, LocalDateTime endTime, String description, String timelength, Integer orderby, Long courseId, Long coursePubId, Integer status, String isPreview, LocalDateTime createDate, LocalDateTime changeDate, TeachplanMedia teachplanMedia) {
        this.id = id;
        this.pname = pname;
        this.parentid = parentid;
        this.grade = grade;
        this.mediaType = mediaType;
        this.startTime = startTime;
        this.endTime = endTime;
        this.description = description;
        this.timelength = timelength;
        this.orderby = orderby;
        this.courseId = courseId;
        this.coursePubId = coursePubId;
        this.status = status;
        this.isPreview = isPreview;
        this.createDate = createDate;
        this.changeDate = changeDate;
        this.teachplanMedia = teachplanMedia;
        this.teachPlanTreeNodes=new ArrayList<>();
    }
    public void addChild(TeachplanDto child) {
        teachPlanTreeNodes.add(child);
    }

    public static List<TeachplanDto> buildHierarchy(List<TeachplanDto> nodeList) {
        Map<Long, TeachplanDto> nodeMap = new HashMap<>();
        List<TeachplanDto> hierarchy = new ArrayList<>();
        for (TeachplanDto node : nodeList) {
            nodeMap.put(node.getId(), node);
        }
        for (TeachplanDto node : nodeList) {

            //判断是top字段是不是1，如果是，就加入根节点，如果不是就加入子节点
            if (node.getParentid()==0) {
                hierarchy.add(node);
            } else {
                TeachplanDto parent = nodeMap.get(node.getParentid());
                if (parent != null) {
                    parent.addChild(node);
                }
            }
        }
        return hierarchy;
    }
}
